package com.rp.system.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rp.common.utils.NullIs;
import com.rp.common.utils.Selectway;
import com.rp.system.common.entity.TypeLibrary;
import com.rp.system.common.mapper.TypeLibraryMapper;
import com.rp.system.common.service.TypeLibraryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author vicente
 * @since 2021-03-20
 */
@Service
public class TypeLibraryServiceImpl extends ServiceImpl<TypeLibraryMapper, TypeLibrary> implements TypeLibraryService {
    /**
     * 位置编码上限 [A-Z]99999
     */
    final public static Integer BUILD_MAX_SIZE = 5;

    @Autowired
    private TypeLibraryMapper typeLibraryMapper;

    @Autowired
    private Selectway selectway;

    @Override
    public boolean save(TypeLibrary typeLibrary) {
        String parentId = typeLibrary.getParentId();
        String code = "";
        char zf = 'A';
        if(parentId == null){
            Integer size = typeLibraryMapper.selectCount(new QueryWrapper<TypeLibrary>().eq("length(tl_code)", BUILD_MAX_SIZE+1));
            code = zf + String.format("%0" + BUILD_MAX_SIZE + "d", size + 1);
            typeLibrary.setTlName(typeLibrary.getName());
        }else{
            TypeLibrary typeLibraryParent = typeLibraryMapper.selectOne(new QueryWrapper<TypeLibrary>().select("tl_name","tl_code").eq("id", parentId));

            typeLibrary.setTlName(getTlNameAndAddress(typeLibraryParent.getTlName(),typeLibrary.getName()));

            int codeLenth = typeLibraryParent.getTlCode().length() + BUILD_MAX_SIZE + 1;
            Integer size = typeLibraryMapper.selectCount(new QueryWrapper<TypeLibrary>()
                    .eq("length(tl_code)", codeLenth)
                    .likeRight("tl_code", typeLibraryParent.getTlCode()));
            code = typeLibraryParent.getTlCode() + (char)(zf+(codeLenth/BUILD_MAX_SIZE - 1)) + String.format("%0" + BUILD_MAX_SIZE + "d", size + 1);
        }
        typeLibrary.setTlCode(code);
        return super.save(typeLibrary);
    }

    @Override
    public boolean updateById(TypeLibrary typeLibrary) {
        System.out.println(typeLibrary);
        String parentId = typeLibrary.getParentId();

        //修改之前的整个对象
        TypeLibrary oldTypeLibrary = typeLibraryMapper.selectOne(new QueryWrapper<TypeLibrary>().select("parent_id,tl_code").eq("id", typeLibrary.getId()));

        if(!NullIs.isEmpty(parentId) && !parentId.equals(oldTypeLibrary.getParentId())){
            //修改了上级
            TypeLibrary oldHigTypeLibrary = typeLibraryMapper.selectOne(new QueryWrapper<TypeLibrary>()
                    .select("tl_name","tl_code")
                    .eq("id", oldTypeLibrary.getParentId()));
            //现在上级  : A01B01
            TypeLibrary newHigTypeLibrary = typeLibraryMapper.selectOne(new QueryWrapper<TypeLibrary>().select("tl_name","tl_code").eq("id", parentId));

            typeLibrary.setTlName(getTlNameAndAddress(newHigTypeLibrary.getTlName(),typeLibrary.getName()));

            System.out.println(oldTypeLibrary.getTlCode());
            System.out.println(oldHigTypeLibrary.getTlCode());
            System.out.println(newHigTypeLibrary.getTlCode());
            //根据以前的code查询 找出 %A01B02C02_% 修改成 A01B01C02
            String now = oldTypeLibrary.getTlCode().replace(oldHigTypeLibrary.getTlCode(), newHigTypeLibrary.getTlCode());
            List<TypeLibrary> typeLibrarys = typeLibraryMapper.selectList(new QueryWrapper<TypeLibrary>().likeRight("tl_code", oldTypeLibrary.getTlCode()));
            for(TypeLibrary l : typeLibrarys){
                l.setTlCode(l.getTlCode().replace(oldTypeLibrary.getTlCode(), now));
                typeLibraryMapper.updateById(l);
            }
            typeLibrary.setTlCode(now);
            //查询出以前的 A01B01C02 所有  替换成   A01B02C02
        }else{
            typeLibrary.setTlName(typeLibrary.getName());
        }

        return super.updateById(typeLibrary);
    }

    /**
     * @Author HuJingYu
     * @Date 17:03 2020-12-24
     * @Description 判断name不为空就加上 -Name
     * @param str1
     * @param str2
     * @return java.lang.String
     */
    private String getTlNameAndAddress(String str1,String str2){
        if(NullIs.isEmpty(str1)){
            if(NullIs.isEmpty(str2)){
                return "";
            }
            return str2;
        }else{
            if(NullIs.isEmpty(str2)){
                return str1;
            }
            return str1 + "-" + str2;
        }

    }

    @Override
    public String findtlCodeById(String id) {
        return typeLibraryMapper.findtlCodeById(id);
    }

    @Override
    public IPage<TypeLibrary> page(IPage<TypeLibrary> page, Wrapper<TypeLibrary> queryWrapper) {
        IPage<TypeLibrary> pagelist = super.page(page, queryWrapper);

//        long beginTime = System.currentTimeMillis();
//        //执行方法
//        List<TypeLibrary> list = pagelist.getRecords();
//        for(TypeLibrary typeLibrary : list){
//            if(!NullIs.isEmpty(typeLibrary.getExpand())){
//                Map<String, Object> map = JSON.parseObject(typeLibrary.getExpand(), Map.class);
//                typeLibrary.setExpandMap(map);
//            }
//        }
//        pagelist.setRecords(list);
//        //执行时长(毫秒)
//        long time = System.currentTimeMillis() - beginTime;
//        System.out.println("TypeLibraryServiceImpl json转换执行时长(毫秒):" + time);

        return pagelist;
    }
}
